package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.Card;
import com.biomatch.domain.CardType;
import com.biomatch.dto.Pagination;
import com.biomatch.mapper.CardMapper;
import com.biomatch.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.tools.tree.NotEqualExpression;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {

    @Autowired
    CardMapper cardMapper;

    @Override
    public IPage<Card> getAllByPage(Pagination pagination) {

        Page<Card> page = new Page<>(pagination.getCurrent(), pagination.getPageSize());
        return cardMapper.getAllByPage(page);
    }

    @Override
    public List<Card> getAll() {
        return cardMapper.selectList(null);
    }

    @Override
    public Card saveOrUpdate(Card card) {
        //增加
        if(card.getId()<=0)
        {
            QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
            cardQueryWrapper.eq("cardName",card.getCardName());
            if(cardMapper.selectCount(cardQueryWrapper)>0)
            {
                return null;
            }
            else {
                String code = String.valueOf(new Date().getTime());
                card.setCardId("C"+code.substring(5,code.length()));
                cardMapper.insert(card);
                return  card;
            }
        }
        //更新
        else {
            QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
            cardQueryWrapper.eq("cardName",card.getCardName())
            .ne("cardId",card.getCardId());
            if(cardMapper.selectCount(cardQueryWrapper)>0)
            {
                return null;
            }
            else{
                cardMapper.updateById(card);
                return  card;
            }
        }
    }

    @Override
    public Card delete(Card card) {
        QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
        cardQueryWrapper.eq("cardId",card.getCardId() );
        if (cardMapper.selectCount(cardQueryWrapper) > 0) {
            cardMapper.deleteById(card.getId());
            return card;
        } else
        {
            return null;
        }
    }

    @Override
    public Card stopCard(Card card) {

        //设置停卡时间
        card.setUpdateTime(new Date());

        //课卡状态
        card.setStop(false);


        //更新
        if(card.getId()>0)
        {
            cardMapper.updateById(card);
        }
        return card;
    }

    @Override
    public Card openCard(Card card) {

        //设置停卡时间
        Date date = new Date();
        long newDate = date.getTime();
        //获得停卡时间
        Date updateTime = card.getUpdateTime();
        long stopDate = updateTime.getTime();
        //相差时间
        long difference = newDate - stopDate;
        //end时间
        long EndTime = card.getEndTime().getTime();
        //更新结束时间 往后推迟
        long newEndTime =EndTime+difference;
        //变成日期
        card.setEndTime(new Date(newEndTime));

        //上次停卡的时间变null
        card.setOpenTime(new Date());
        //课卡状态
        card.setStop(true);
        //次数减少一次
        card.setStopCount(card.getStopCount()-1);

        //更新
        if(card.getId()>0)
        {
            cardMapper.updateById(card);
        }
        return card;
    }
}
